let mysqlConnection = require("./mysqlConfig")
let Utils = require("../dao/Utils")

module.exports = {
    selectUserTasks: function (userId, res) {
        return new Promise((resolve,reject) => {
            let selectUserTasksSQL = `SELECT * FROM tasks.user_tasks where user_id = ${userId}`;
            mysqlConnection.query(selectUserTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                }
                else {resolve(results);}
            })
        })
    },
    insertUserTasks: function (user_id, tasks_id) {
        return new Promise((resolve,reject) => {
            let insertUserTasksSQL = `INSERT INTO tasks.user_tasks (user_id, tasks_id, status, submited_time) VALUES (${user_id}, ${tasks_id}, 0, null)`;
            mysqlConnection.query(insertUserTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                }
                else {resolve(results);}
            })
        })
    },
    updateUserTasks: function (userId, tasksId, description,status, submitedTime) {
        return new Promise((resolve,reject) => {
            let updateUserTasksSQL = `UPDATE tasks.user_tasks t SET status = 1 , t.submit_description = '${description}' ,t.submited_time = ${submitedTime}  WHERE t.user_id = ${userId} AND t.tasks_id = ${tasksId}`
            mysqlConnection.query(updateUserTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                }
                else {resolve(results);}
            })
        })
    },
    updateUserTasksFileId: function (userId, tasksId,fileId) {
        return new Promise((resolve,reject) => {
            let updateUserTasksSQL = `UPDATE tasks.user_tasks t SET   t.file_id = '${fileId} WHERE t.user_id = ${userId} AND t.tasks_id = ${tasksId}`
            mysqlConnection.query(updateUserTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                }
                else {resolve(results);}
            })
        })
    },


/*
    updateUserNotice: function (isNotice,userId,tasksId) {
        return new Promise((resolve,reject) => {
            let updateUserNoticeSQL = `UPDATE tasks.user_tasks t SET t.isNotice = ${isNotice} WHERE t.user_id = ${userId} AND t.tasks_id = ${tasksId}`
            console.log(updateUserNoticeSQL)
            mysqlConnection.query(updateUserNoticeSQL, function (error, results) {
                if (error) {
                    reject(error)
                }
                else {resolve(results);}
            })
        })
    },*/
    deleteUserTask: function (userId, tasksId) {
        return new Promise((resolve,reject) => {
            let deleteUserTaskSQL = `DELETE FROM tasks.user_tasks WHERE user_id = ${userId} AND tasks_id = ${tasksId}`
            console.log(deleteUserTaskSQL)
            mysqlConnection.query(deleteUserTaskSQL, function (error, results) {
                if (error) {
                    reject(error)
                }
                else {resolve(results);}
            })
        })
    },
    selectUserTodayTasks: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTodayTasksSQL = `
       select at.id, at.title, at.description,at.catagory, at.due_date,at.marks

from user_tasks ut
join all_tasks at
    on ut.tasks_id = at.id
  
    where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}&&ut.user_id = ${id}
      order by at.id
    `
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTodayTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTodayTasksDescTaskID: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTodayTasksSQL = `
       select at.id, at.title, at.description,at.catagory, at.due_date,at.marks

from user_tasks ut
join all_tasks at
    on ut.tasks_id = at.id
  
    where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}&&ut.user_id = ${id}
      order by at.id desc
    `
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTodayTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTodayTasksTimeEsc: function (id) {
        return new Promise((resolve, reject) => {
            let selectUserTodayTasksSQL = `
       select at.id, at.title, at.description,at.catagory, at.due_date,at.marks

from user_tasks ut
join all_tasks at
    on ut.tasks_id = at.id
    where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}&&ut.user_id = ${id}
      order by at.id desc
    `
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTodayTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTodayTasksTimeDesc: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTodayTasksSQL = `
       select at.id, at.title, at.description,at.catagory, at.due_date,at.marks

from user_tasks ut
join all_tasks at
    on ut.tasks_id = at.id
   
    where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()} && ut.user_id = ${id}
     order by due_date desc
    `
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTodayTasksSQL, function (error, results) {
                console.log(selectUserTodayTasksSQL)
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTodayTasksWithDescription: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTodayTasksSQL = `
       select at.id, at.title, at.catagory, at.description, at.due_date,at.marks

from user_tasks ut
join all_tasks at
    on ut.tasks_id = at.id
    where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}&&ut.user_id = ${id}`
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`
            mysqlConnection.query(selectUserTodayTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTomorrowTasks: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTomorrowTasksSQL = `
       select at.id, at.title, at.catagory, at.due_date,at.marks
        from user_tasks ut
        join all_tasks at
    on ut.tasks_id = at.id
    where due_date >= ${Utils.TommorrowZeroTime()} && due_date < ${Utils.TheDayAfterTommorrowZeroTime()}&&ut.user_id = ${id}`
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTomorrowTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTomorrowTasksEscTime: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTomorrowTasksSQL = `
       select at.id, at.title, at.catagory, at.due_date,at.marks
        from user_tasks ut
        join all_tasks at
    on ut.tasks_id = at.id
    where due_date >= ${Utils.TommorrowZeroTime()} && due_date < ${Utils.TheDayAfterTommorrowZeroTime()}&&ut.user_id = ${id}
    order by due_date
    `
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTomorrowTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
    selectUserTomorrowTasksDescTime: function (id) {
        return new Promise((resolve, reject) => {

            let selectUserTomorrowTasksSQL = `
       select at.id, at.title, at.catagory, at.due_date,at.marks
        from user_tasks ut
        join all_tasks at
    on ut.tasks_id = at.id
    where due_date >= ${Utils.TommorrowZeroTime()} && due_date < ${Utils.TheDayAfterTommorrowZeroTime()}&&ut.user_id = ${id}
    order by due_date desc`
            // let selectTodayTasksSQl2=`select * from all_tasks where due_date >= ${Utils.TodayZeroTime()} && due_date < ${Utils.TommorrowZeroTime()}`

            mysqlConnection.query(selectUserTomorrowTasksSQL, function (error, results) {
                if (error) {
                    reject(error)
                } else {
                    resolve(results);
                }
            })
        })
    },
}

